#import "CTGuidePageControl.h"
@interface CTGuidePageControl ()
@property (nonatomic ,strong) NSMutableArray *viewArray;
@property (nonatomic ,strong) UIView *contentView;
@property (nonatomic ,strong) NSMutableArray *focusColorArr;
@property (nonatomic ,strong) NSArray *originColorArr;
@end
@implementation CTGuidePageControl
static CGFloat itemHeight = 8;
static CGFloat longWidth = 8;
static CGFloat shortWidth = 8;
static CGFloat offset = 10;
#pragma mark - systemMethods
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.originColorArr = @[[UIColor color8080FF],[UIColor color8080FF],[UIColor color8080FF],[UIColor color8080FF]];
        [self setSubViews];
        [self setConstraints];
    }
    return self;
}
#pragma mark - privateMethods
- (void)setSubViews
{
    [self addSubview:self.contentView];
}
- (void)setItemView
{
    [self.viewArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [(UIView *)obj removeFromSuperview];
    }];
    [self.viewArray removeAllObjects];
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(longWidth + (self.numberOfPages - 1) * (shortWidth + offset));
    }];
    NSArray *colorArr = self.focusColorArr;
    UIView *lastView;
    for (int i = 0; i < self.numberOfPages; i ++) {
        UIView *view = [self itemViewForColor:colorArr[i]];
        [self.contentView addSubview:view];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = itemHeight*0.5;
        CGFloat width = shortWidth;
        if (i == self.currentPage) {
            width = longWidth;
        }
        else{
            if (self.grayColor) {
                view.backgroundColor = self.grayColor;
            }
        }
        if (lastView) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastView.mas_right).offset(offset);
                make.size.mas_equalTo(CGSizeMake(width, itemHeight));
                make.centerY.equalTo(self.contentView.mas_centerY);
            }];
        }
        else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.size.mas_equalTo(CGSizeMake(width, itemHeight));
                make.centerY.equalTo(self.contentView.mas_centerY);
            }];
        }
        [self.viewArray addObject:view];
        lastView = view;
    }
}
- (UIView *)itemViewForColor:(UIColor *)color
{
    UIView *view = [UIView new];
    view.backgroundColor = color;
    return view;
}
- (void)setConstraints
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(20);
    }];
}
#pragma mark - event handler
#pragma mark - getters and setters
- (void)setCurrentPage:(NSInteger)currentPage
{
    _currentPage = currentPage;
    [self.viewArray enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (currentPage == idx) {
            [UIView animateWithDuration:0.15 delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{
                [obj mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(longWidth);
                }];
                [self layoutIfNeeded];
            } completion:nil];
            obj.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
        }
        else{
            [UIView animateWithDuration:0.15 delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{
                [obj mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(shortWidth);
                }];
                [self layoutIfNeeded];
            } completion:nil];
                if (self.grayColor) {
                    obj.backgroundColor = self.grayColor;
                }
            }
    }];
}
- (void)setNumberOfPages:(NSInteger)numberOfPages
{
    _numberOfPages = numberOfPages;
    [self.focusColorArr removeAllObjects];
    for (int i = 0; i < self.numberOfPages; i++) {
        [self.focusColorArr addObject:self.originColorArr[i%(self.originColorArr.count)]];
    }
    [self setItemView];
}
- (NSMutableArray *)viewArray
{
    if (_viewArray) {
        return _viewArray;
    }
    _viewArray = [NSMutableArray new];
    return _viewArray;
}
- (UIView *)contentView
{
    if (_contentView) {
        return _contentView;
    }
    _contentView = [UIView new];
    return _contentView;
}
- (NSMutableArray *)focusColorArr
{
    if (_focusColorArr) {
        return _focusColorArr;
    }
    _focusColorArr = [NSMutableArray array];
    return _focusColorArr;
}
@end
